<?php

namespace App\Http\Controllers;

use App\Models\Question;
use App\Models\Reply;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\DB;
use JPush\Client as JPush;

class ReplyController extends BaseController
{
    public function addReply(Request $request){
        $reply=new Reply;
        $reply->questionId=$request->input('questionId');
        $reply->userId=$request->input('userId');
        $reply->content=$request->input('content');

        $reply->save();

        $user=User::find($reply->userId);
        $question=Question::find($reply->questionId);
        $client = new JPush("49a057407f2e5590104e6e01", "88a7d48da19c80b0c185c716");
        if($user->type==0){
            $client->push()
                ->setPlatform('all')
                ->addAllAudience()
                ->setNotificationAlert($user->name.'回答了 '.$question->title.' 问题')
                ->send();
        }else{
            $client->push()
                ->setPlatform('all')
                ->addAllAudience()
                ->setNotificationAlert($user->name.'老师回答了 '.$question->title.' 问题')
                ->send();
        }


        $arr = array('status' => 'success', 'data' => $reply->id);
        echo json_encode($arr);
    }

    public function getAllReplysById(){
        $questionId=$_GET['questionId'];
        $replys=DB::table('reply')         //将两张表拼接起来
        ->where('reply.questionId', $questionId)
        ->join('user', function($join)
            {
                $join->on('reply.userId', '=', 'user.id');
            })->select( 'user.name','reply.content')
            ->get();
        $arr = array('status' => 'success', 'data' => $replys);
        echo json_encode($arr);
    }

    /*public function getOneQuestion(Request $request){
        $question=Question::find($request->input('id'));
        $arr = array('status' => 'success', 'data' => $question);
        echo json_encode($arr);
    }*/

}
